<template>
	<view>
		{{voicePath}}
		<button @tap="startRecord">开始录音</button>
		<button @tap="endRecord">停止录音</button>
		<button @tap="playVoice">播放录音</button>
		<button @tap="upload">上传</button>
		
		<button @tap="uploadImage">上传</button>
	</view>
</template>

<script>
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();

	innerAudioContext.autoplay = true;

	export default {
		data() {
			return {
				text: 'uni-app',
				voicePath: ''
			}
		},
		onLoad() {
			let self = this;
			recorderManager.onStop(function(res) {
				console.log('recorder stop' + JSON.stringify(res));
				self.voicePath = res.tempFilePath;
			});
		},
		methods: {
			uploadImage() {
				uni.chooseMedia({
					count: 1,
					success: resp => {
						console.log(resp)
					}
				})
			},
			startRecord() {
				console.log('开始录音');

				recorderManager.start();
			},
			endRecord() {
				console.log('录音结束');
				recorderManager.stop();
			},
			playVoice() {
				console.log('播放录音');

				if (this.voicePath) {
					innerAudioContext.src = this.voicePath;
					innerAudioContext.play();
				}
			},
			upload() {
				uniCloud.uploadFile({
					filePath: this.voicePath,
					cloudPath: '1.mp3',
					success: resp => {
						console.log(resp)
					}
				})
			}
		}
	}
</script>

<style lang="scss">

</style>